package com.situ.day11;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class CalDemo {
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		frame.add(panel);
		//设置布局
		panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		//创建组件
		//创建文本框
		JTextField textField1 = new JTextField();
		textField1.setPreferredSize(new Dimension(120, 30));
		panel.add(textField1);
		//+号
		JLabel label1 = new JLabel("+");
		label1.setPreferredSize(new Dimension(30, 30));
		panel.add(label1);
		//文本框2
		JTextField textField2 = new JTextField();
		textField2.setPreferredSize(new Dimension(120,30));
		panel.add(textField2);
		// =button
		JButton button = new JButton("=");
		panel.add(button);
		//label 结果
		JLabel resultJLabel = new JLabel();
		panel.add(resultJLabel);
		
		//给button添加点击事件 匿名内部类
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String num1 = textField1.getText();
				
				String num2 = textField2.getText();
				
				int result = Integer.parseInt(num1) + Integer.parseInt(num2);
				resultJLabel.setText(Integer.toString(result));
			}
		});
		frame.setResizable(false);
	    frame.setSize(500, 300);
	    frame.setLocation(500, 200);
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setVisible(true);
	}
}
